/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : TextManipulator.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 16, 2010  mycodetalks			       Created
 */
package lwf.util;

import java.util.Map;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;

import lwf.factory.PropertyReader;

/**
 * Class description goes here
 */
public class TextManipulator {
	
	private static PropertyReader propertyreader = PropertyReader.getInstance();
	
	   public static String replacePH(String sSource, String phname, int quantity) {
	    	
	    	Map mapSource = getPhData(phname);
	    	String newString = sSource;
	    	String replace = null;
	    	String find = "[" + phname + "_";
	        //Check
	    	if(sSource.indexOf(find) == -1) {
	    		return sSource;
	    	}

	    	if(quantity > 1) {
	        	String[] srandom = RandomNumberGenerator.generate(1, mapSource.size(), quantity);
	        	for(int i=0; i<srandom.length; i++) {
	        		find = "[" + phname + "_" + String.valueOf(i+1) + "]";
	        		replace = (String) mapSource.get(srandom[i]);
	        		newString = StringManipulator.replace(newString, find, replace);
	        	}    		
	    	}
	    	else {
	    		find = "[" + phname + "_1]";
	    		String srand = String.valueOf(RandomNumberGenerator.generate(1, mapSource.size()));
	    		replace = (String) mapSource.get(srand);
	    		newString = StringManipulator.replace(newString, find, replace);
	    	}

	    	return newString;
	    }
	    
	   public static  String replaceValues(String sSource, String[] values) {
	    	String newString = sSource;
	    	for(int i=0; i<values.length; i++) {
	    		String find = "[VALUE_" + String.valueOf(i+1) + "]";
	    		String replace = values[i];
	    		newString = StringManipulator.replace(newString, find, replace);
	    	}
	    	return newString;
	    }
	    
	   private static  Map getPhData(String phname) {
	    	if (phname.equals(IHandlerConstant.DAY)) {
	    		return propertyreader.getDays();
	    	} 
	    	else if (phname.equals(IHandlerConstant.ANIMAL)) {
	    		return propertyreader.getAnimals();
	    	}
	    	else if (phname.equals(IHandlerConstant.ITEM)) {
	    		return propertyreader.getItems();
	    	}    
	    	else if (phname.equals(IHandlerConstant.LIQUID)) {
	    		return propertyreader.getLiquids();
	    	}    
	    	else if (phname.equals(IHandlerConstant.NAME)) {
	    		return propertyreader.getNames();
	    	}
	    	else if (phname.equals(IHandlerConstant.COLOR)) {
	    		return propertyreader.getColors();
	    	}
	    	else if (phname.equals(IHandlerConstant.FRUITS)) {
	    		return propertyreader.getFruits();
	    	}
	    	return null;
	    }

}



